package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.forms.SubscriptionCancelForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common;
import vcp.client.utility.UtilityHelper;
import vcp.entity.Car;
import vcp.entity.Subscription;
import vcp.enums.SubscriptionType;

/**
 * <p>
 * user interface account status screen view class
 * </p>
 * 
 * @author VCP team
 * 
 */
public class UIAccStatusView extends UIAbstractView implements ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfSubscriptionType;
	private JTextField tfBalance;
	private JTextField tfStartDate;
	private JTextField tfEndDate;
	private JTextField tfSite;
	private JComboBox<Integer> cbSubscriptionID;
	private JComboBox<Integer> cbCars;

	private JButton btnCancelSubscription;

	private IFormSubmitionListener<SubscriptionCancelForm> formSubmtionListener;

	/**
	 * setter
	 * 
	 * @param formSubmtionListener
	 */
	public void setFormSubmtionListener(IFormSubmitionListener<SubscriptionCancelForm> formSubmtionListener)
	{
		this.formSubmtionListener = formSubmtionListener;
	}

	/**
	 * constructor
	 */
	public UIAccStatusView()
	{
		super();
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * getter
	 * 
	 * @return current on screen subscription number
	 */
	public Integer getCurrentSubscription()
	{
		return (Integer) this.cbSubscriptionID.getSelectedItem();
	}

	public Integer getCurrentCar()
	{
		return (Integer) this.cbCars.getSelectedItem();
	}

	/**
	 * loads one subscription
	 * 
	 * @param info
	 */
	public void loadOneSubscription(Subscription info)
	{
		this.btnCancelSubscription.setEnabled(info.getSubscriptionType() == SubscriptionType.ORDER_AHEAD);

		this.tfSite.setText(info.getSite() != null ? info.getSite().toString() : "All");
		this.tfSubscriptionType.setText(info.getSubscriptionType().toString());

		DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm");
		this.tfEndDate.setText(dtf.print(info.getEndDate()));
		this.tfStartDate.setText(dtf.print(info.getStartDate()));

		this.cbCars.removeAllItems();
		for (Car car : info.getCars())
		{
			Integer carID = car.getCarID();
			this.cbCars.addItem(carID);
		}
	}

	/**
	 * loads customer balance
	 * 
	 * @param balance
	 */
	public void loadBalance(float balance)
	{
		this.tfBalance.setText(String.valueOf(balance));
	}

	/**
	 * loads list of subscriptions
	 * 
	 * @param info
	 */
	public void loadSubscriptions(List<Subscription> info)
	{
		if (info.isEmpty())
		{
			this.resetView();
			this.btnCancelSubscription.setEnabled(false);
			this.cbSubscriptionID.setEnabled(false);
			this.cbCars.setEnabled(false);
		}
		else
		{
			this.cbSubscriptionID.setEnabled(true);
			this.cbCars.setEnabled(true);

			for (Subscription subscription : info)
			{
				Integer subscribitionID = subscription.getSubscribtionID();
				this.cbSubscriptionID.addItem(subscribitionID);
			}
			this.loadOneSubscription(info.get(0));
		}
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void resetView()
	{
		this.tfSubscriptionType.setText("");
		this.tfBalance.setText("");
		this.tfStartDate.setText("");
		this.tfEndDate.setText("");
		this.tfSite.setText("");

		this.cbCars.removeAllItems();
		this.cbSubscriptionID.removeAllItems();
	}

	@Override
	public void clearView()
	{
		this.resetView();
		this.setOnScreen(false);
		this.btnCancelSubscription.setEnabled(false);
		this.cbSubscriptionID.setEnabled(false);
		this.cbCars.setEnabled(false);
	}

	@Override
	protected void setupLayout()
	{
		this.setPreferredSize(new Dimension(904, 708));
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.WEST);

		JLabel lblSubscription = new JLabel("Subscription:");
		lblSubscription.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel.add(lblSubscription);

		this.cbSubscriptionID = new JComboBox<>();
		this.cbSubscriptionID.setModel(new DefaultComboBoxModel<Integer>());
		this.cbSubscriptionID.setFocusable(false);
		this.cbSubscriptionID.setPreferredSize(new Dimension(100, 30));
		this.cbSubscriptionID.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel.add(this.cbSubscriptionID);

		JPanel panel_1 = new JPanel();
		this.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(10, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_7 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_7.getLayout();
		flowLayout.setVgap(10);
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_3.add(panel_7);

		JLabel lblNewLabel = new JLabel("Subscription Type:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_7.add(lblNewLabel);

		JPanel panel_8 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_8.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_8);

		this.tfSubscriptionType = new JTextField();
		this.tfSubscriptionType.setEnabled(false);
		this.tfSubscriptionType.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_8.add(this.tfSubscriptionType);
		this.tfSubscriptionType.setColumns(10);

		JPanel panel_5 = new JPanel();
		panel_1.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_11 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_11.getLayout();
		flowLayout_5.setVgap(10);
		flowLayout_5.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_11);

		JLabel lblStartSubscriptionDate = new JLabel("Start Subscription Date:");
		lblStartSubscriptionDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_11.add(lblStartSubscriptionDate);

		JPanel panel_12 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_12.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_12);

		this.tfStartDate = new JTextField();
		this.tfStartDate.setEnabled(false);
		this.tfStartDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_12.add(this.tfStartDate);
		this.tfStartDate.setColumns(10);

		JPanel panel_6 = new JPanel();
		panel_1.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_13 = new JPanel();
		FlowLayout flowLayout_6 = (FlowLayout) panel_13.getLayout();
		flowLayout_6.setVgap(10);
		flowLayout_6.setAlignment(FlowLayout.RIGHT);
		panel_6.add(panel_13);

		JLabel lblEndSubscriptionDate = new JLabel("End Subscription Date:");
		lblEndSubscriptionDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_13.add(lblEndSubscriptionDate);

		JPanel panel_14 = new JPanel();
		FlowLayout flowLayout_7 = (FlowLayout) panel_14.getLayout();
		flowLayout_7.setAlignment(FlowLayout.LEFT);
		panel_6.add(panel_14);

		this.tfEndDate = new JTextField();
		this.tfEndDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.tfEndDate.setEnabled(false);
		panel_14.add(this.tfEndDate);
		this.tfEndDate.setColumns(10);

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_15 = new JPanel();
		FlowLayout flowLayout_8 = (FlowLayout) panel_15.getLayout();
		flowLayout_8.setVgap(10);
		flowLayout_8.setAlignment(FlowLayout.RIGHT);
		panel_2.add(panel_15);

		JLabel lblSite = new JLabel("Site:");
		lblSite.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_15.add(lblSite);

		JPanel panel_16 = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel_16.getLayout();
		flowLayout_9.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_16);

		this.tfSite = new JTextField();
		this.tfSite.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.tfSite.setEnabled(false);
		panel_16.add(this.tfSite);
		this.tfSite.setColumns(10);

		JPanel panel_17 = new JPanel();
		panel_1.add(panel_17);
		panel_17.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_18 = new JPanel();
		FlowLayout flowLayout_10 = (FlowLayout) panel_18.getLayout();
		flowLayout_10.setVgap(10);
		flowLayout_10.setAlignment(FlowLayout.RIGHT);
		panel_17.add(panel_18);

		JLabel lblCars = new JLabel("Cars:");
		lblCars.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_18.add(lblCars);

		JPanel panel_19 = new JPanel();
		FlowLayout flowLayout_11 = (FlowLayout) panel_19.getLayout();
		flowLayout_11.setAlignment(FlowLayout.LEFT);
		panel_17.add(panel_19);

		this.cbCars = new JComboBox<>();
		this.cbCars.setModel(new DefaultComboBoxModel<Integer>());
		this.cbCars.setFocusable(false);
		this.cbCars.setPreferredSize(new Dimension(200, 35));
		this.cbCars.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_19.add(this.cbCars);

		JPanel panel_21 = new JPanel();
		panel_1.add(panel_21);

		JPanel panel_22 = new JPanel();
		panel_1.add(panel_22);

		JPanel panel_20 = new JPanel();
		panel_1.add(panel_20);
		panel_20.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		JLabel label = new JLabel("Total account Balance:");
		panel_20.add(label);
		label.setFont(new Font("Tahoma", Font.PLAIN, 18));

		this.tfBalance = new JTextField();
		panel_20.add(this.tfBalance);
		this.tfBalance.setEnabled(false);
		this.tfBalance.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.tfBalance.setColumns(10);

		JPanel panel_9 = new JPanel();
		panel_1.add(panel_9);

		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_10 = new JPanel();
		panel_10.setPreferredSize(new Dimension(10, 60));
		this.add(panel_10, BorderLayout.SOUTH);
		panel_10.setLayout(new GridLayout(0, 5, 0, 0));

		JPanel panel_23 = new JPanel();
		panel_10.add(panel_23);

		JPanel panel_24 = new JPanel();
		panel_10.add(panel_24);

		JPanel panel_25 = new JPanel();
		panel_10.add(panel_25);

		JPanel panel_26 = new JPanel();
		panel_10.add(panel_26);

		this.btnCancelSubscription = new JButton(Common.BTN_CANCEL_SUBSCRIPTION);
		this.btnCancelSubscription.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		panel_10.add(this.btnCancelSubscription);
	}

	@Override
	protected void setupListeners()
	{
		this.btnCancelSubscription.addActionListener(this);
		this.cbSubscriptionID.addItemListener(this);
	}

	@Override
	protected void setupButtonsIcons()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 40, 40);
		if (icon != null)
		{
			this.btnCancelSubscription.setIcon(icon);
			this.btnCancelSubscription.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed(ActionEvent actionEvent)
	{
		if (this.formSubmtionListener != null)
		{
			SubscriptionCancelForm form = new SubscriptionCancelForm(this.getCurrentSubscription(), this.getCurrentCar());
			this.formSubmtionListener.onFormSubmitted(new FormSubmitionEvent<>(this, form));
		}
	}

	/****************************************************************************************************************************
	 *** ItemListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void itemStateChanged(ItemEvent event)
	{
		if (event.getStateChange() == ItemEvent.SELECTED)
		{
			if (this.onScreen)
			{
				if (this.controllerDelegate != null)
				{
					Integer selectedSubscription = (Integer) event.getItem();
					if (selectedSubscription != this.getCurrentSubscription())
					{
						this.controllerDelegate.onButtonPress(null);
					}
				}
			}
		}
	}

}
